﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MobileAPI.Web.Data;
using MobileAPI.BLL;
using MobileAPI.Entity.Model;
using MobileAPI.Common;

namespace MobileAPI.Web.APIs
{
    [MobileAPI(Code = "MC0001", Name = "用户登录")]
    public class Login : IProcessMessage
    {
        private BLLLogin bllULogin;
        public BLLLogin BllUlogin
        {
            get
            {
                if (bllULogin == null)
                    bllULogin = BLLFactory.getInstance().CreateLoginBLL();
                return bllULogin;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                string LoginName = reqMsg.Body.GetFieldValue("LoginName");
                string Password = reqMsg.Body.GetFieldValue("Password");
                if (string.IsNullOrEmpty(LoginName) || string.IsNullOrEmpty(Password))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                EUser user = BllUlogin.UserLogin(LoginName, Password);
                if (user == null)
                {
                    rspMsg.Head.RspCode  = MessageCode.Login_Faild.Code;
                    rspMsg.Head.RspMsg = MessageCode.Login_Faild.Msg;

                    return rspMsg;
                }
                else
                {
                    // 返回内容（具体请参见文档）
                    rspMsg.Body.AddField("UserID",user.UserID.TString());
                    rspMsg.Body.AddField("LoginName", user.LoginName);
                    rspMsg.Body.AddField("Password", user.Password);
                    rspMsg.Body.AddField("UserName", user.UserName);
                    rspMsg.Body.AddField("Description", user.Description);
                    rspMsg.Body.AddField("UserParam", user.UserParam);
                    rspMsg.Body.AddField("UserSite", user.UserSite);

                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                    return rspMsg;
                }
            }
            catch (Exception e)
            {

                ErrorLogWriter.Write("Login-" + e);
                throw e;
            }

        }
        #endregion
    }
}